package com.dianping.cat.home.utilization.transform;
import com.dianping.cat.home.utilization.IVisitor;
import com.dianping.cat.home.utilization.entity.ApplicationState;
import com.dianping.cat.home.utilization.entity.Domain;
import com.dianping.cat.home.utilization.entity.MachineState;
import com.dianping.cat.home.utilization.entity.UtilizationReport;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  16 */     this.m_linker = new DefaultLinker(true);




/*  21 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static UtilizationReport parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static UtilizationReport parse(InputStream in) {
    DefaultNativeParser parser = new DefaultNativeParser(in);
/*  30 */     UtilizationReport utilizationReport = new UtilizationReport();

    try {
/*  33 */       utilizationReport.accept(parser);
/*  34 */     } catch (RuntimeException e) {
/*  35 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  38 */         throw e;
      }
    }

/*  42 */     parser.m_linker.finish();
/*  43 */     return utilizationReport;
  }



  public void visitApplicationState(ApplicationState applicationState) {
    byte tag;
/*  50 */     while ((tag = readTag()) != -1) {
/*  51 */       visitApplicationStateChildren(applicationState, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitApplicationStateChildren(ApplicationState applicationState, int _field, int _type) {
/*  56 */     switch (_field) {
      case 1:
/*  58 */         applicationState.setId(readString());
        break;
      case 2:
/*  61 */         applicationState.setFailureCount(readLong());
        break;
      case 3:
/*  64 */         applicationState.setFailurePercent(readDouble());
        break;
      case 4:
/*  67 */         applicationState.setCount(readLong());
        break;
      case 5:
/*  70 */         applicationState.setMaxQps(readDouble());
        break;
      case 6:
/*  73 */         applicationState.setAvg(readDouble());
        break;
      case 7:
/*  76 */         applicationState.setSum(readDouble());
        break;
      case 8:
/*  79 */         applicationState.setAvg95(readDouble());
        break;
    }
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  88 */     while ((tag = readTag()) != -1) {
/*  89 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  94 */     switch (_field) {
      case 1:
/*  96 */         domain.setId(readString());
        break;
      case 2:
/*  99 */         domain.setMachineNumber(readInt());
        break;
      case 3:
/* 102 */         domain.setCmdbId(readString());
        break;
      case 33:
/* 105 */         if (_type == 1) {
/* 106 */           MachineState machineState = new MachineState();

/* 108 */           visitMachineState(machineState);
/* 109 */           this.m_linker.onMachineState(domain, machineState); break;
/* 110 */         }  if (_type == 2) {
/* 111 */           for (int i = readInt(); i > 0; i--) {
/* 112 */             MachineState machineState = new MachineState();

/* 114 */             visitMachineState(machineState);
/* 115 */             this.m_linker.onMachineState(domain, machineState);
          }
        }
        break;
      case 34:
/* 120 */         if (_type == 1) {
/* 121 */           ApplicationState applicationState = new ApplicationState();

/* 123 */           visitApplicationState(applicationState);
/* 124 */           this.m_linker.onApplicationState(domain, applicationState); break;
/* 125 */         }  if (_type == 2) {
/* 126 */           for (int i = readInt(); i > 0; i--) {
/* 127 */             ApplicationState applicationState = new ApplicationState();

/* 129 */             visitApplicationState(applicationState);
/* 130 */             this.m_linker.onApplicationState(domain, applicationState);
          }
        }
        break;
    }
  }



  public void visitMachineState(MachineState machineState) {
    byte tag;
/* 141 */     while ((tag = readTag()) != -1) {
/* 142 */       visitMachineStateChildren(machineState, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitMachineStateChildren(MachineState machineState, int _field, int _type) {
/* 147 */     switch (_field) {
      case 1:
/* 149 */         machineState.setId(readString());
        break;
      case 2:
/* 152 */         machineState.setCount(readLong());
        break;
      case 3:
/* 155 */         machineState.setSum(readDouble());
        break;
      case 4:
/* 158 */         machineState.setAvg(readDouble());
        break;
      case 5:
/* 161 */         machineState.setAvgMax(readDouble());
        break;
    }
  }



  public void visitUtilizationReport(UtilizationReport utilizationReport) {
    byte tag;
/* 170 */     if ((tag = readTag()) != -4) {
/* 171 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 174 */     while ((tag = readTag()) != -1) {
/* 175 */       visitUtilizationReportChildren(utilizationReport, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitUtilizationReportChildren(UtilizationReport utilizationReport, int _field, int _type) {
/* 180 */     switch (_field) {
      case 1:
/* 182 */         utilizationReport.setDomain(readString());
        break;
      case 2:
/* 185 */         utilizationReport.setStartTime(readDate());
        break;
      case 3:
/* 188 */         utilizationReport.setEndTime(readDate());
        break;
      case 33:
/* 191 */         if (_type == 1) {
/* 192 */           Domain domain = new Domain();

/* 194 */           visitDomain(domain);
/* 195 */           this.m_linker.onDomain(utilizationReport, domain); break;
/* 196 */         }  if (_type == 2) {
/* 197 */           for (int i = readInt(); i > 0; i--) {
/* 198 */             Domain domain = new Domain();

/* 200 */             visitDomain(domain);
/* 201 */             this.m_linker.onDomain(utilizationReport, domain);
          }
        }
        break;
    }
  }

  private Date readDate() {
    try {
/* 210 */       return new Date(readVarint(64));
/* 211 */     } catch (IOException e) {
/* 212 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 218 */       return Double.longBitsToDouble(readVarint(64));
/* 219 */     } catch (IOException e) {
/* 220 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 226 */       return (int)readVarint(32);
/* 227 */     } catch (IOException e) {
/* 228 */       throw new RuntimeException(e);
    }
  }

  private long readLong() {
    try {
/* 234 */       return readVarint(64);
/* 235 */     } catch (IOException e) {
/* 236 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 242 */       return this.m_in.readUTF();
/* 243 */     } catch (IOException e) {
/* 244 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 250 */       return this.m_in.readByte();
/* 251 */     } catch (IOException e) {
/* 252 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 257 */     int shift = 0;
/* 258 */     long result = 0L;

/* 260 */     while (shift < length) {
/* 261 */       byte b = this.m_in.readByte();
/* 262 */       result |= (b & 0x7F) << shift;
/* 263 */       if ((b & 0x80) == 0) {
/* 264 */         return result;
      }
/* 266 */       shift += 7;
    }

/* 269 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/utilization/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */